<template>
  <div class="food">
    <div class="food-nav">
      <van-tabs v-model:active="activeName">
        <van-tab to="/home" title="精选"></van-tab>
        <van-tab
          v-for="item in foodList"
          :title="item.name"
          :key="item.id"
          :name="item.name"
          :to="{
            name: 'food',
            params: {
              cid: item.cid,
            },
            query: {
              title: item.name,
            },
          }"
        >
          <div class="cateList" v-for="e in item.sub_class" :key="e.id">
            <ul class="list">
              <router-link
                :to="{
                  name: 'catelist',
                  params: {
                    cid: e.original_id,
                  },
                  query: {
                    title: e.name,
                  },
                }"
              >
                <li class="item">
                  <a href="" class="item_box">
                    <img :src="e.icon" alt="" />
                    <span>{{ e.name }}</span>
                  </a>
                </li>
              </router-link>
            </ul>
          </div>
        </van-tab>
      </van-tabs>
    </div>
    <div class="food-body">
      <van-tabs v-model:active="active" swipeable @click="OnPush">
        <van-tab
          v-for="item in tabs"
          :title="item.title"
          :name="item.name"
          :key="item.id"
        >
          <div class="list" v-for="item in list" :key="item.id">
            <a href="">
              <div class="list-img">
                <img :src="item.pic" alt="" />
              </div>
            </a>
            <div class="cent">
              <a href="" class="title">
                <div class="product_title">
                  <span class="tianmao"></span>
                  <span class="title_txt">{{ item.d_title }}</span>
                </div>
              </a>
              <div class="product_info">
                <a href="" class="info">
                  <div class="price">
                    <span>券后&nbsp;</span>
                    <span class="RMB">¥</span>
                    <span class="price_num">{{ item.jiage }}</span>
                  </div>
                  <div class="label_box">
                    <span class="box">
                      <span
                        class="label2"
                        v-for="(e, i) in item.label"
                        :key="i"
                        >{{ e.title }}</span
                      >
                      <span class="juan">
                        <span>劵</span>{{ item.quan_jine }}元
                      </span>
                    </span>
                  </div>
                  <div class="salse">
                    <span>已售{{ item.xiaoliang }}</span
                    ><span style="display: none">评论379</span>
                  </div>
                </a>
                <div class="shop_name">
                  <a href="?r=p/shop&amp;seller_id=3697280864"
                    >{{ item.shop_name
                    }}<span>进店<van-icon name="arrow" /></span
                  ></a>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script lang="ts">
import {
  computed,
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  watch,
} from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
export default defineComponent({
  setup() {
    const store = useStore();
    const foodList = computed(() => {
      return store.state.food.foods;
    });
    const list = computed(() => {
      return store.state.food.list;
    });
    const route = useRoute();
    const activeName = route.query.title;
    const state: any = reactive({
      tabs: [
        {
          id: 1,
          title: "人气",
          name: "t",
        },
        {
          id: 2,
          title: "最新",
          name: "latest",
        },
        {
          id: 3,
          title: "销量",
          name: "sell",
        },
        {
          id: 4,
          title: "价格",
          name: "price",
        },
      ],
      heightFlag: false,
      setTOP: 410,
      initHeight: "",
      cid: "",
    });
    watch(route, () => {
      if (state.cid != route.params.cid) {
        state.cid = route.params.cid;
        store.dispatch({
          type: "food/getFooditem",
          cid: state.cid,
        });
      }
    });

    onMounted(() => {
      OnPush("t");
      store.dispatch({
        type: "food/getFoodList",
      });
    });

    const OnPush = (name: any) => {
      store.dispatch({
        type: "food/getFooditem",
        cid: state.cid,
        name,
      });
    };

    return {
      OnPush,
      foodList,
      list,
      activeName,
      ...toRefs(state),
    };
  },
});
</script>

<style lang="stylus" scoped>
html, body, .food {
  height: 100%;
  width: 100%;
}

ul, li {
  list-style: none;
}

* {
  padding: 0;
  margin: 0;
}

.food-nav {
  /deep/.van-tabs__wrap {
    height: 0.44rem;
    z-index: 80;
    zoom: 1;
    position: fixed;
    top: 44px;
    width: 100%;
    background: #fff;
    transform-origin: center;
    text-align: center;
  }

  .nullbox {
    height: 0.88rem;
    width: 100%;
  }

  /deep/.van-tabs__content {
    background: #fff;
    border-bottom: solid 7px #f5f5f5;
    padding: 0.84rem 0 0.1rem 0;
  }
}

/deep/.cateList {
  .list {
    .item {
      width: 25%;
      float: left;
      text-align: center;
      -webkit-transition: all 0.6s cubic-bezier(0.4, 0.68, 0.15, 1) 0s;
      -webkit-transform: translateY(0) translateX(0);
      transform: translateY(0) translateX(0);
      opacity: 1;

      .item_box {
        display: block;
        padding-top: 10px;
        height: 0.93rem;
        width: 0.93rem;

        img {
          display: block;
          width: 60%;
          margin: auto;
        }

        span {
          color: #333;
          font-size: 12px;
          display: block;
          line-height: 30px;
        }
      }
    }
  }
}

.food-body {
  position: sticky;
  top: 0px;
  margin-top: 0px;

  /deep/.van-tabs__wrap {
    width: 100%;
    z-index: 60;
  }

  /deep/.van-tabs__content {
    width: 100%;
    padding-top: 0.05rem;
  }

  div {
    margin: 0;
    border: none;
    padding: 0;
    outline: 0;
  }

  .list {
    height: 1.34rem;
    padding: 0.07rem 0 0 0.07rem;
    transform-origin: center;
    position: relative;
    z-index: 1;
    zoom: 1;
    border-bottom: 1px solid #F5F5F5;

    .list-img {
      float: left;
      position: relative;
      z-index: 1;
      zoom: 1;
      margin-right: 0.1rem;

      img {
        height: 1.26rem;
        width: 1.26rem;
        display: block;
        border-radius: 0.03rem;
      }
    }

    .cent {
      height: 1.46rem;
      position: relative;
      z-index: 1;
      zoom: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-space-around;

      .title {
        .product_title {
          font-size: 0.14rem;
          font-weight: 400;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          align-items: center;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
          line-height: 0.2rem;
          padding-right: 0.1rem;
          margin-bottom: 0.3rem;

          .tianmao {
            display: inline-block;
            background: url('../../assets/tm.png');
            background-size: 23px 13px;
            height: 0.13rem;
            width: 0.23rem;
            margin-right: 0.04rem;
          }
        }
      }

      .price {
        font-size: 0.1rem;
        font-weight: 400;
        color: #666;
      }

      .shop_name {
        font-size: 0.1rem;
        font-weight: 400;
        color: #888;
        top: 1.1rem;
        position: absolute;

        a {
          color: #888;
          margin-right: 0.04rem;

          span {
            color: #333;
            margin-left: 0.04rem;
          }
        }
      }

      .salse {
        span {
          font-size: 0.1rem;
          font-weight: 400;
          color: #888;
        }
      }

      .product_info {
        width: 100%;
        height: 0.78rem;
      }

      .RMB {
        word-break: break-all;
        list-style: none;
        border: none;
        font-size: 0.12rem;
        font-weight: 500;
        color: #FF2B22;
        margin-left: 0.01rem;
      }

      .price_num {
        word-break: break-all;
        list-style: none;
        border: none;
        font-size: 0.19rem;
        font-weight: 500;
        color: #FF2B22;
      }

      .salse {
        height: 0.2rem;
      }

      .label_box {
        margin-right: 2px;
        height: 0.2rem;

        .label2 {
          height: 12px;
          border-radius: 2px;
          border: 1px solid;
          font-size: 9.5px;
          font-weight: 400;
          color: #F57223;
          padding: 0 3px;
          margin-right: 5px;
          display: inline-block;
          line-height: 12px;
          vertical-align: middle;
          border-color: rgba(245, 114, 36, 0.7);
        }

        .box {
          display: inline;
          height: 0.2rem;

          .juan {
            height: 0.14rem;
            background: linear-gradient(90deg, #ff8873 0, #ff4f4f 100%);
            border-radius: 2px;
            padding: 0 0.05rem 0 0.01rem;
            font-size: 0.1rem;
            font-weight: 400;
            color: #fff;
            line-height: 14px;
            display: inline-block;
            position: relative;
            z-index: 1;
            zoom: 1;
            top: 0;
            vertical-align: middle;
          }
        }
      }
    }
  }

  p {
    margin: 0;
    padding: 0;
  }

  a {
    text-decoration: none;
    color: #333;
  }
}

/deep/.van-tabs__content {
  overflow-y: scroll;
  height: 100%;
}

a {
  text-decoration: none;
  color: #333;
}
</style>
